package com.jd.sunny.homework;

/**
 * @author :Sun Lin
 * @Description: 输出金字塔结构
 * @date :2012-11-23
 * @since:1.0.0
 */
public class PringPyramid {

    /**
     * currentNumber
     */
    private static int currentNumber = 2;



    /**
     * @param args
     */
    public static void main(String[] args) {
        int a = 10;
//        for (int i = 1; i <= a; i++) {
//            print(i, a);
//        }
//        System.out.println("******************************************");
//        for (int i = 1; i <= a; i++) {
//            print2(i, a);
//        }
//        System.out.println("******************************************");
        for (int i = 1; i <= a; i++) {
            printPrimeNumber(i, a);
        }
    }


    /**
     * 打印
     *
     * @param lineNumber
     * @param totalLineNumber
     */
    public static void print(int lineNumber, int totalLineNumber) {
        //先输出totalLineNumber - lineNumber 个 Tab
        for (int i = 0; i < totalLineNumber - lineNumber; i++) {
            System.out.print("\t");
        }
        //输出lineNumber个lineNumber，每个数字中间两个Tab
        for (int i = 0; i < lineNumber; i++) {
            System.out.print(lineNumber);
            System.out.print("\t\t");
        }

        //输出换行
        System.out.print("\n");
    }

    /**
     * 第二种打印
     *
     * @param lineNumber
     * @param totalLineNumber
     */
    public static void print2(int lineNumber, int totalLineNumber) {

        int number = (lineNumber * lineNumber - lineNumber + 2) / 2;
        //先输出totalLineNumber - lineNumber 个 Tab
        for (int i = 0; i < totalLineNumber - lineNumber; i++) {
            System.out.print("\t");
        }
        //输出lineNumber个数字，从number开始，每个数字中间输出两个Tab
        for (int i = 0; i < lineNumber; i++) {
            System.out.print(number);
            System.out.print("\t\t");
            number++;
        }

        //输出换行
        System.out.print("\n");
    }

    /**
     * 第三种打印
     *
     * @param lineNumber
     * @param totalLineNumber
     */
    public static void printPrimeNumber(int lineNumber, int totalLineNumber) {

        //先输出totalLineNumber - lineNumber 个 Tab
        for (int i = 0; i < totalLineNumber - lineNumber; i++) {
            System.out.print("\t");
        }

        //输出lineNumber个数字，从Currentumber开始，每个数字中间输出两个Tab
        int printCount = 0;
        while (printCount < lineNumber){
            //判断是否为质数,是则输出，并记录本行已打印数字个数，否则丢弃
            if(isPrime(currentNumber)){
                System.out.print(currentNumber);
                System.out.print("\t\t");
                printCount++;
            }
            //当前数字每次调整 
            currentNumber++;
        }
        //输出换行
        System.out.print("\n");
    }

    /**
     * 判断是否为质数
     * @param l
     * @return
     */
    private static boolean isPrime(long l){
        boolean  flag = true;
        if(l==2){
            return true;
        }
        for(long i = 2;i <= (l/2) + 1;i++){
            if( (l%i)== 0){
                flag = false;
                break;
            }
        }
        return flag;
    }
}
